package com.maochang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maochang.mapper.PsUserMapper;
import com.maochang.pojo.PsUser;
import com.maochang.service.PsUserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service("psUserServiceImpl")
public  class psUserServiceImpl extends ServiceImpl<PsUserMapper, PsUser> implements PsUserService {
    /**
     * 登录成功
     */
    @Resource
    public PsUserMapper PsUserMapper;

    @Override
    public PsUser searchByUserCode2(String userCode){
        QueryWrapper<PsUser> queryWrapper= Wrappers.query();
        queryWrapper.eq("userCode",userCode);
        return PsUserMapper.selectOne(queryWrapper) ;
    };
    @Override
    public List<PsUser> ChaXunJiAn() {
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq("roleId","3").select("id","userName");
        return  PsUserMapper.selectList(wrapper);
    }
    @Override
    public Boolean UpdatePwd(PsUser psUser) {
        QueryWrapper<PsUser> wrapper = Wrappers.query();
        wrapper.eq("id",psUser.getId());
        String keYiA=psUser.getSex();
        if(keYiA.equals("on")){
            psUser.setSex("男");
        }else {
            psUser.setSex("女");
        }
        int  row=   PsUserMapper.update(psUser,wrapper);
        boolean Falg=false;
        if(row>0){
            Falg=true;
        }
        return  Falg;
    }

    @Override
    public Page<PsUser> getUserAll(int current,int page,String userName,String userCode ) {
        Page<PsUser> psPIPage=new Page<>(current,page);
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq(!StringUtils.isBlank(userName),"userName",userName)
               .eq(!StringUtils.isBlank(userCode),"userCode",userCode);
        Page<PsUser> psUserPage = PsUserMapper.getAllUser(psPIPage,wrapper);
        return psUserPage;
    }

    @Override
    public Boolean UpDateZhuAngTai(Integer id,String FAlg) {
        PsUser psUser=new PsUser();
        psUser.setUserStatus((FAlg.equals("true"))?1:0);
        System.out.println(FAlg+"ddd");
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq("id",id);
        int  count = PsUserMapper.update(psUser,wrapper);
        Boolean isCheNgGOng=false;
        if(count>0){
            isCheNgGOng=true;
        }
        return isCheNgGOng;
    }

    @Override
    public Boolean UpdatePsUserMessage(PsUser psUser) {
        QueryWrapper<PsUser> wrapper = Wrappers.query();
        wrapper.eq("id",psUser.getId());
        int update = PsUserMapper.update(psUser, wrapper);
        if(update>0){
            return true;
        }
        return false;
    }

    @Override
    public Boolean ShanChuYongHu(Integer id) {
        QueryWrapper<PsUser> wrapper = Wrappers.query();
        wrapper.eq("id",id);
        int delete = PsUserMapper.delete(wrapper);
        return delete>0?true:false;
    }

    @Override
    public Boolean addUser(PsUser psUser) {
        //解决密码问题
        System.out.println(psUser.getUserPassword()+"qian");
        String encodedPassword2 = new SimpleHash("md5",psUser.getUserPassword(),"qwert",1).toString();
        psUser.setUserPassword(encodedPassword2);
        System.out.println(encodedPassword2+"后");
        //用户状态
        psUser.setUserStatus(1);
        psUser.setSalt("qwert");
        //最后修改时间
        psUser.setLastLoginTime(LocalDateTime.now());
        //入职时间
        psUser.setEntryDate(LocalDate.now());
        int insert = PsUserMapper.insert(psUser);
        return insert>0?true:false;
    }

    @Override
    public List<PsUser> xiaoQuYuan() {
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq("roleId","2").select("id","userName");
        return  PsUserMapper.selectList(wrapper);
    }

    @Override
    public List<PsUser> zhenZhengDeXunJiYuan() {
        QueryWrapper<PsUser> wrapper= Wrappers.query();
        wrapper.eq("roleId","4").select("id","userName");
        return  PsUserMapper.selectList(wrapper);
    }

}
